import React from 'react';
import { Info, KDlg, obj_int, KInput, ck_int, loc, useKForm } from 'sui';
import { is_box } from 'test/base/cmd';

const DlgVal = (props) => {
    const form = useKForm(props.data);

    const ck = {
        num : v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const n = parseInt(v, 10);
            const num = obj_int(props.data, "num");
            if (n > num) {
                return loc("l_707");
            }

            return null;
        }
    };

    return (
        <KDlg {...props} title={loc("l_779")} ck={ck} form={form}>
            <KInput label={loc("l_127")} form={form} name="num"/>
        </KDlg>
    );
};

const DlgNum = (props) => {
    const form = useKForm(props.data);

    const ck = {
        num: v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const tmp = parseInt(v, 10);
            const num = obj_int(props.data, "num");

            if (tmp > num) {
                return loc("l_707");
            }

            if (is_box(props.mod)) {
                const snum = obj_int(props.data, "snum");
                const count = snum * obj_int(form.value, "box");

                if (tmp !== count) {
                    return loc("l_110");
                }
            }

            return null;
        },
    };

    if (is_box(props.mod)) {
        ck["box"] = v => ck_int(v);
    }

    const getTitle = () => {
        if (props.fin === "fin") {
            return loc("l_105");
        }

        return loc("l_111");
    };

    const getView = () => {
        if (is_box(props.mod)) {
            return <KInput label={loc("l_452")} form={form} name="box"/>;
        }

        return null;
    };

    return (
        <KDlg {...props} title={getTitle()} ck={ck} form={form}>
            <Info data={props.data} col={props.col} hl={["num"]}/>
            <KInput label={loc("l_127")} form={form} name="num"/>
            {getView()}
        </KDlg>
    );
};

export {DlgVal, DlgNum};
